import json

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect

# 这里只做数据传输，不做数据处理,初始化处理数据的逻辑放到GM_init

# 构造房源
from myapp.GM_build import build
from myapp.GM_getSteps import *
from myapp.GM_init import *


# 进入房源构造页面
def house(request):
    '构造房源'
    res = {}

    # 带入初始化数据（可用，可不用）
    # 这里只是演示，实际应该是通过request请求或者读取配置文件等获取初始化的数据,最后以列表嵌套字典的形式存储
    # res['address'] = [{"name": "bj"}, {"name": "sh"}]
    res['address'] = Init_house_get_address()
    res["steps"] = DB_GM_steps.objects.filter(tool_id=1).order_by("order")

    # 增加步骤的请求需要用到，steps里面无法直接获取
    res["GM_tool_id"] = 1
    return render(request, "GM_tools/house.html", res)


# 房源校验输入用户名密码
def house_check_user(request):
    username = request.GET["username"]
    password = request.GET.get("password")
    # print(username,password)
    res = Init_house_check_user(username, password)
    return JsonResponse(res)


# 房源-构造
def house_run(request):
    user_data = {}  # 需要把前段传递来的所有数据都发给业务层处理，不可能一个个加，所以用字典来存储
    # 把所有数据拿出来
    user_data["input_username"] = request.GET['input_username']
    user_data["input_userpwd"] = request.GET['input_userpwd']
    user_data["house_name"] = request.GET['house_name']
    user_data["house_price"] = request.GET['house_price']
    user_data["people_counts"] = request.GET['people_counts']
    user_data["furniture"] = request.GET['furniture']
    user_data["city"] = request.GET['city']
    user_data["country"] = request.GET['country']
    print(f" 前端传递来的 user_data ==> {user_data}")
    # 规范数据（根据需求来看）
    user_data["country"] = user_data["country"].lower()
    # 过滤步骤
    del_filter = []  # 需要删除的步骤过滤标记

    if user_data["country"] == "china":
        del_filter.append("del_country")

    if user_data["city"] == "cz":
        del_filter.append("del_city")

    # 获取步骤
    steps = get_steps(tool_id=1, del_filter=del_filter)
    # print(steps)

    # 调用业务层真实构造
    res = build(user_data, steps)
    # print(res)
    # 返回最终结果
    return HttpResponse(json.dumps(res), content_type="application/json")
    # return HttpResponse("")


def add_step(request, tid):
    # print(tid)
    count = len(DB_GM_steps.objects.filter(tool_id=tid))  # 把order 设置为10的倍数，这样可以随意在每个步骤的前后插入新的步骤
    DB_GM_steps.objects.create(tool_id=tid, order=(count + 1) * 10, name="新增第%s步骤（待设置)" % ((count + 1) * 10))

    if tid == "1":
        # return house(request)  这种写法是错误的，点击后页面的地址还是a标签跳转的地址，必须要用重定向 redirect
        return redirect("/GM_tools/house/")

    return


# 进入订单构造页面
def order(request):
    '构造房源'
    res = {}

    # 带入初始化数据（可用，可不用）
    # 这里只是演示，实际应该是通过request请求或者读取配置文件等获取初始化的数据,最后以列表嵌套字典的形式存储
    # res['address'] = [{"name": "bj"}, {"name": "sh"}]
    # res['address'] = Init_house_get_address()
    res["steps"] = DB_GM_steps.objects.filter(tool_id=1).order_by("order")

    # 增加步骤的请求需要用到，steps里面无法直接获取
    res["GM_tool_id"] = 2
    return render(request, "GM_tools/order.html", res)


# 订单-构造
def order_run(request):
    user_data = {}  # 需要把前段传递来的所有数据都发给业务层处理，不可能一个个加，所以用字典来存储
    # 把所有数据拿出来
    user_data["input_username"] = request.GET['input_username']
    user_data["input_userpwd"] = request.GET['input_userpwd']
    user_data["house_name"] = request.GET['house_name']
    user_data["house_price"] = request.GET['house_price']
    user_data["people_counts"] = request.GET['people_counts']
    user_data["furniture"] = request.GET['furniture']
    user_data["city"] = request.GET['city']
    user_data["country"] = request.GET['country']
    print(f" 前端传递来的 user_data ==> {user_data}")
    # 规范数据（根据需求来看）

    # 过滤步骤
    del_filter = []  # 需要删除的步骤过滤标记
    #
    # if user_data["country"] == "china":
    #     del_filter.append("del_country")
    #
    # if user_data["city"] == "cz":
    #     del_filter.append("del_city")

    # 获取步骤
    steps = get_steps(tool_id=2, del_filter=del_filter)
    # print(steps)

    # 调用业务层真实构造
    res = build(user_data, steps)
    # print(res)
    # 返回最终结果
    return HttpResponse(json.dumps(res), content_type="application/json")
    # return HttpResponse("")



